package com.util;

import com.se.http.HttpMethod;
import sun.misc.Unsafe;
import java.lang.reflect.Field;

/**
 * @Author: hdu_huang
 * @Date: 2021/7/22 16:01
 */
public class UnsafeUtil {

    public static Unsafe unsafe;

    static {
        Field f;
        try {
            f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            unsafe = (Unsafe) f.get(null);
        } catch (Exception ignored) {
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T newObj(Class<T> type) {
        try {
           return (T)unsafe.allocateInstance(type);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        HttpMethod method = newObj(HttpMethod.class);
        System.out.println(method);
    }

}
